<?php
/**
 * 金币商城管理
 */
namespace Coin\Controller;

use Common\Controller\AdminbaseController;

class AdminCoinController extends AdminbaseController {

    protected $coin_model;

    function _initialize() {
        parent::_initialize();
        $this->coin_model = D("Coin/CoinGoods");
    }

    // 后台金币商城管理列表
    public function index(){
        $this->_lists(array("goods_status"=>array('neq',-1)));
        $this->display();
    }

    // 金币商城添加
    public function add(){

        $this->display();
    }

    // 金币商城添加提交
    public function add_post(){

        if (IS_POST) {
            if(!empty($_POST['photos_alt']) && !empty($_POST['photos_url'])){
                foreach ($_POST['photos_url'] as $key=>$url){
                    $photourl=sp_asset_relative_url($url);
                    $_POST['smeta']['photo'][]=array("url"=>$photourl,"alt"=>$_POST['photos_alt'][$key]);
                }
            }
            $_POST['smeta']['thumb'] = sp_asset_relative_url($_POST['smeta']['thumb']);

            $_POST['post']['update_time']=date("Y-m-d H:i:s",time());

            $coin=I("post.post");
            $coin['goods_pics']=json_encode($_POST['smeta']);
            $coin['goods_description']=htmlspecialchars_decode($coin['goods_description']);

            $result=$this->coin_model->add($coin);

            if ($result) {
                $this->success("添加成功！");
            } else {
                $this->error("添加失败！");
            }

        }
    }

    // 金币商城编辑
    public function edit(){
        $id=  I("get.id",0,'intval');

        $post=$this->coin_model->where("id=$id")->find();

        $this->assign("post",$post);
        $this->assign("smeta",json_decode($post['goods_pics'],true));

        $this->display();
    }

    // 金币商城编辑提交
    public function edit_post(){

        if (IS_POST) {
            $post_id=intval($_POST['post']['id']);

            if(!empty($_POST['photos_alt']) && !empty($_POST['photos_url'])){
                foreach ($_POST['photos_url'] as $key=>$url){
                    $photourl=sp_asset_relative_url($url);
                    $_POST['smeta']['photo'][]=array("url"=>$photourl,"alt"=>$_POST['photos_alt'][$key]);
                }
            }
            $_POST['smeta']['thumb'] = sp_asset_relative_url($_POST['smeta']['thumb']);

            $_POST['post']['update_time']=date("Y-m-d H:i:s",time());
            $coin=I("post.post");
            $coin['goods_pics']=json_encode($_POST['smeta']);
            $coin['goods_description']=htmlspecialchars_decode($coin['goods_description']);
            $result=$this->coin_model->save($coin);
            if ($result!==false) {
                $this->success("保存成功！");
            } else {
                $this->error("保存失败！");
            }
        }
    }

    // 金币商城排序
    public function listorders() {
        $status = parent::_listorders();
        if ($status) {
            $this->success("排序更新成功！");
        } else {
            $this->error("排序更新失败！");
        }
    }

    /**
     * 金币商城列表处理方法,根据不同条件显示不同的列表
     * @param array $where 查询条件
     */
    private function _lists($where=array()){
        // $cate_id=I('request.term',0,'intval');

        // $where['act_type']=array(array('eq',1),array('exp','IS NULL'),'OR');

        // if(!empty($cate_id)){
        //     $where['b.cate_id']=$cate_id;
        //     $term=$this->category_model->where(array('cate_id'=>$cate_id))->find();
        //     $this->assign("term",$term);
        // }

        $start_time=I('request.start_time');
        if(!empty($start_time)){
            $where['create_time']=array(
                array('EGT',$start_time)
            );
        }

        $end_time=I('request.end_time');
        if(!empty($end_time)){
            if(empty($where['create_time'])){
                $where['create_time']=array();
            }
            array_push($where['create_time'], array('ELT',$end_time));
        }

        $keyword=I('request.keyword');
        if(!empty($keyword)){
            $where['goods_name']=array('like',"%$keyword%");
        }

        $this->coin_model
        ->alias("a")
        ->where($where);

        $count=$this->coin_model->count();

        $page = $this->page($count, 20);

        $this->coin_model
        ->alias("a")
        ->where($where)
        ->limit($page->firstRow , $page->listRows)
        ->order("a.create_time DESC");

        $posts=$this->coin_model->select();

        $this->assign("page", $page->show('Admin'));
        $this->assign("formget",array_merge($_GET,$_POST));
        $this->assign("posts",$posts);
    }


    // 金币商城删除
    public function delete(){
        if(isset($_GET['id'])){
            $id = I("get.id",0,'intval');
            if ($this->coin_model->where(array('id'=>$id))->save(array('goods_status'=>"-1")) !==false) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }

        if(isset($_POST['ids'])){
            $ids = I('post.ids/a');

            if ($this->coin_model->where(array('id'=>array('in',$ids)))->save(array('goods_status'=>"-1"))!==false) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }
    }

    // 兑换码核销
    public function verify(){
        $this->display();
    }

    public function verify_post(){
        if (IS_POST) {

            $where=['convert_code'=>$_POST['convert_code']];

            $verify = I('post.');
            $data['status']='2';
            $data['remark']=$verify['remark'];
            $data['update_time']=date("Y-m-d H:i:s",time());

            $res = M('CoinGoodsRecord')->where($where)->find();

            if($res){
                $result=M('CoinGoodsRecord')->where($where)->save($data);
                if ($result!==false) {
                    $this->success("核销成功！");
                } else {
                    $this->error("核销失败！");
                }
            }else{
                $this->error("核销失败！");
            }

        }
    }

}